import json
import time

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
}

params = {
    'ibc': 'newspc',
    'limit': 30,
    'showLevelThreshold': 72,
    'headLimit': 1,
    'tailLimit': 2,
    'offset': 0,
    'callback': 'jsonp_1620967839416',
    '_': int(round(time.time() * 1000))
}


class Wangyi:
    def __init__(self, serial_no):
        self.serial_no = serial_no
        self.url = f'https://comment.api.163.com/api/v1/products/a2869674571f77b5a0867c3d71db5856/threads/{serial_no}/comments/newList'

    def _get_answer_count(self):
        """
        查找该新闻的总回答数
        :return: 回答数
        """
        params['offset'] = 0
        r = requests.get(url=self.url, headers=headers, params=params)
        print(r.text[20:-1])
        data = json.loads(r.text[20:-1])
        r.close()
        return data['newListSize']

    def get_content_list(self):
        """
        :param serial_no: 新闻序号
        :return: 评论数组
        """
        func = lambda x: x // 30 if not x % 30 else x // 30 + 1
        content_count = self._get_answer_count()
        print('总评论数: %d' % content_count)
        page_size = func(content_count)
        res = {}
        for page in range(page_size):
            print('第%d/%d页爬取成功' % (page + 1, page_size))
            params['offset'] = page * 30
            r = requests.get(url=self.url, headers=headers, params=params)
            data = json.loads(r.text[20:-1])
            comments = data['comments']
            for k in comments:
                res[k] = comments[k]['content']
        return res


if __name__ == '__main__':
    wangyi = Wangyi('GD9MQKQ300019K82')
    values = wangyi.get_content_list()
    print(values)
    # with open('文件/%s.txt' % serial_no, 'w', encoding='utf-8') as f:
    #     for k in values:
    #         f.write(values[k] + '\n')
